package org.example.core.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.example.core.constant.TokenConstants;

import java.util.Date;
import java.util.Map;

/**
 * @author 小杰_cc
 * @version 1.0
 */

/**
 * jwt工具类
 */
public class JwtUtils {

    // 管理员过期时间
    private final static long expireTime = TokenConstants.EXPIRATION * 60 * 1000;

    // 会员过期时间
    private final static long member_expireTime = TokenConstants.MEMBER_EXPIRATION * 24 * 60 *60 * 1000;

    // 刷新的时间间隔
    private final static long refreshTime = TokenConstants.REFRESH_TIME * 60 * 1000;

    // 创建管理人员token令牌
    public static String createUserToken(Map<String, Object> claims){
        String token = Jwts.builder()
                .expiration(new Date(System.currentTimeMillis() + expireTime))
                .claims(claims).signWith(TokenConstants.USER_SECRET, Jwts.SIG.HS512).compact();
        return token;
    }

    // 创建会员token令牌
    public static String createMemberToken(Map<String, Object> claims){
        String token = Jwts.builder()
                .expiration(new Date(System.currentTimeMillis() + member_expireTime))
                .claims(claims).signWith(TokenConstants.MEMBER_SECRET, Jwts.SIG.HS512).compact();
        return token;
    }

    // 获取管理人员令牌中的数据
    public static Claims getUserClaims(String token){
        try {
            return Jwts.parser().verifyWith(TokenConstants.USER_SECRET).build().parseSignedClaims(token).getPayload();
        } catch (Exception e) {
//            e.printStackTrace();
            return null;
        }
    }

    // 获取管理人员令牌中的数据
    public static Claims getMemberClaims(String token){
        try {
            return Jwts.parser().verifyWith(TokenConstants.MEMBER_SECRET).build().parseSignedClaims(token).getPayload();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 管理人员用户id
    public static String getUserId(Claims claims){
        return String.valueOf(claims.get(TokenConstants.USER_ID));
    }

    public static Long getLongUserId(Claims claims){
        return Long.parseLong(String.valueOf(claims.get(TokenConstants.USER_ID)));
    }

    // 获取管理人员用户名
    public static String getUserName(Claims claims){
        return String.valueOf(claims.get(TokenConstants.USER_NAME));
    }

    // 获取管理人员token的过期时间
    public static long getUserExpireTime(Claims claims){
        return claims.getExpiration().getTime();
    }

    // 会员用户id
    public static String getMemberId(Claims claims){
        return String.valueOf(claims.get(TokenConstants.MEMBER_ID));
    }

    public static Long getLongMemberId(Claims claims){
        return Long.parseLong(String.valueOf(claims.get(TokenConstants.MEMBER_ID)));
    }

    // 会员用户名
    public static String getMemberName(Claims claims){
        return String.valueOf(claims.get(TokenConstants.MEMBER_NAME));
    }

    // 会员token的过期时间
    public static long getMemberExpireTime(Claims claims){
        return claims.getExpiration().getTime();
    }

    // 是否要刷新token
    public static boolean isRefresh(Claims claims){
        long timeNow = System.currentTimeMillis();
        long timeAfter = getUserExpireTime(claims);
        return timeAfter - timeNow <= refreshTime;
    }
}